package com.study.talk;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Generator {

    static String projectPath = System.getProperty("user.dir");
    public static void main(String[] args) {

        List<String> tables = new ArrayList<>();
        tables.add("user");
        tables.add("user_info");
        tables.add("friend");
        tables.add("group_info");
        tables.add("group_user_info");

        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/talk", "root", "123456")
                .globalConfig(builder -> {
                    builder.author("thor") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir( projectPath + "/src/main/java"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    builder.dbQuery(new MySqlQuery());
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.study.talk") // 设置父包名
                            .moduleName("module") // 设置父包模块名
                            .controller("controller")
                            .service("service")
                            .entity("entity")
                            .mapper("mapper")
                            .pathInfo(getPathInfo()); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables); // 设置需要生成的表名

                    builder.serviceBuilder().formatServiceFileName("%sService");

                    builder.entityBuilder()
                            .idType(IdType.AUTO)
                    .enableLombok();
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

    private static Map<OutputFile, String> getPathInfo() {
        Map<OutputFile, String> pathInfo = new HashMap<>(5);
        pathInfo.put(OutputFile.entity, projectPath + "/src/main/java/com/study/talk/module/entity");
        pathInfo.put(OutputFile.mapper, projectPath + "/src/main/java/com/study/talk/module/mapper");
        pathInfo.put(OutputFile.service, projectPath + "/src/main/java/com/study/talk/module/service");
        pathInfo.put(OutputFile.serviceImpl, projectPath + "/src/main/java/com/study/talk/module/service/impl");
        pathInfo.put(OutputFile.controller, projectPath + "/src/main/java/com/study/talk/module/controller");
        pathInfo.put(OutputFile.xml, projectPath + "/src/main/resources/mappers");
        return pathInfo;
    }

}
